package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Crawlerfilestatus;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("CrawlerfilestatusController")
public class CrawlerfilestatusController extends CmiEmaController {

	@RequestMapping("/listCrawlerfilestatuss")
	public ModelAndView listCrawlerfilestatuss(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = crawlerfilestatusPaginationService.updatePagination(request, pagination);
		Set<Crawlerfilestatus> crawlerfilestatuss = crawlerfilestatusPaginationService
				.getPaginationCrawlerfilestatuss(pagination);

		mav.addObject("crawlerfilestatuss", crawlerfilestatuss);
		mav.addObject("entityName", "crawlerfilestatus");
		mav.addObject("pagination", pagination);
		mav.setViewName("crawlerfilestatus/listCrawlerfilestatuss");
		return mav;
	}

	public String listCrawlerfilestatuss() {
		return "redirect:/listCrawlerfilestatuss";
	}

	@RequestMapping("/viewCrawlerfilestatus")
	public ModelAndView viewCrawlerfilestatus(@RequestParam Integer crawlerfilestatusId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("crawlerfilestatus", crawlerfilestatusDAO.findCrawlerfilestatusByPrimaryKey(crawlerfilestatusId));
		mav.setViewName("crawlerfilestatus/viewCrawlerfilestatus");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteCrawlerfilestatuss")
	public ModelAndView confirmDeleteCrawlerfilestatuss(
			@RequestParam(value = "deleteCrawlerfilestatussIds", required = true) Set<Integer> deleteCrawlerfilestatussIds) {
		ModelAndView mav = new ModelAndView();

		Set<Crawlerfilestatus> deleteCrawlerfilestatuss = crawlerfilestatusDAO
				.findCrawlerfilestatussByIds(deleteCrawlerfilestatussIds);
		Set<Crawlerfilestatus> referencedCrawlerfilestatuss = crawlerfilestatusService
				.loadReferenceCrawlerfilestatuss(deleteCrawlerfilestatuss);
		boolean hasReferenced;
		if (referencedCrawlerfilestatuss.size() == 0) {
			mav.addObject("deleteEntitys", deleteCrawlerfilestatuss);
			hasReferenced = false;
		} else {
			deleteCrawlerfilestatuss.removeAll(referencedCrawlerfilestatuss);
			mav.addObject("referencedEntitys", referencedCrawlerfilestatuss);
			mav.addObject("deleteEntitys", deleteCrawlerfilestatuss);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Crawlerfilestatus");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listCrawlerfilestatuss");
		return mav;
	}

	@RequestMapping("/deleteCrawlerfilestatus")
	public String deleteCrawlerfilestatus(@RequestParam Integer crawlerfilestatusId) {
		Crawlerfilestatus crawlerfilestatus = crawlerfilestatusDAO
				.findCrawlerfilestatusByPrimaryKey(crawlerfilestatusId);
		crawlerfilestatusService.deleteCrawlerfilestatus(crawlerfilestatus);
		return "forward:/listCrawlerfilestatuss";
	}

	@RequestMapping("/confirmDeleteCrawlerfilestatus")
	public ModelAndView confirmDeleteCrawlerfilestatus(@RequestParam Integer crawlerfilestatusId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("crawlerfilestatus", crawlerfilestatusDAO.findCrawlerfilestatusByPrimaryKey(crawlerfilestatusId));

		mav.setViewName("crawlerfilestatus/deleteCrawlerfilestatus");

		return mav;
	}

	@RequestMapping("/deleteCrawlerfilestatuss")
	public String deleteCrawlerfilestatuss(
			@RequestParam(value = "deleteCrawlerfilestatussId", required = true) Set<Integer> deleteCrawlerfilestatussId) {
		crawlerfilestatusService.deleteCrawlerfilestatuss(deleteCrawlerfilestatussId);
		return "forward:/listCrawlerfilestatuss";
	}

	@RequestMapping("/editCrawlerfilestatus")
	public ModelAndView editCrawlerfilestatus(@RequestParam Integer crawlerfilestatusId) {

		ModelAndView mav = new ModelAndView();

		Crawlerfilestatus crawlerfilestatus = crawlerfilestatusDAO
				.findCrawlerfilestatusByPrimaryKey(crawlerfilestatusId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("crawlerfilestatus", crawlerfilestatus);

		// 把所有的引用crawlerfilestatus对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerfilestatusService.loadCrawlerfilestatusObjectReferencedSet(crawlerfilestatus));

		// 将编辑页面crawlerfilestatus添加到mav中
		mav.setViewName("crawlerfilestatus/editCrawlerfilestatus");
		return mav;
	}

	@RequestMapping("/newCrawlerfilestatus")
	public ModelAndView newCrawlerfilestatus() {
		ModelAndView mav = new ModelAndView();
		Crawlerfilestatus crawlerfilestatus = new Crawlerfilestatus();

		mav.addObject("crawlerfilestatus", crawlerfilestatus);

		// 把所有的引用crawlerfilestatus对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerfilestatusService.loadCrawlerfilestatusObjectReferencedSet(crawlerfilestatus));

		mav.addObject("newFlag", true);
		mav.setViewName("crawlerfilestatus/editCrawlerfilestatus");

		return mav;
	}

	@RequestMapping("/saveCrawlerfilestatus")
	public ModelAndView saveCrawlerfilestatus(@ModelAttribute Crawlerfilestatus crawlerfilestatus) {

		ModelAndView mav = new ModelAndView();

		Crawlerfilestatus existingCrawlerfilestatus = CmiHibernateProxyUtil
				.deproxy(crawlerfilestatusDAO.findCrawlerfilestatusByPhysicalPrimaryKey(crawlerfilestatus.getName()));

		// 新建记录
		if (crawlerfilestatus.getId() == null) {
			if (crawlerfilestatus.equals(existingCrawlerfilestatus)) {

				mav.addAllObjects(crawlerfilestatusService.loadCrawlerfilestatusClassReferencedSet(crawlerfilestatus));
				// mav = editContinue(mav, crawlerfilestatus);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerfilestatus/editCrawlerfilestatus");
			} else {
				crawlerfilestatusService.saveCrawlerfilestatus(crawlerfilestatus);
				mav.setViewName("forward:/listCrawlerfilestatuss");
			}
		}
		// 编辑记录
		else {
			Crawlerfilestatus originCrawlerfilestatus = CmiHibernateProxyUtil
					.deproxy(crawlerfilestatusDAO.findCrawlerfilestatusByPrimaryKey(crawlerfilestatus.getId()));
			if (!crawlerfilestatus.equals(originCrawlerfilestatus)
					&& crawlerfilestatus.equals(existingCrawlerfilestatus)) {
				mav.addAllObjects(crawlerfilestatusService.loadCrawlerfilestatusClassReferencedSet(crawlerfilestatus));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerfilestatus/editCrawlerfilestatus");
			} else {
				crawlerfilestatusService.saveCrawlerfilestatus(crawlerfilestatus);
				mav.setViewName("forward:/listCrawlerfilestatuss");
			}
		}
		return mav;
	}

	@RequestMapping("/crawlerfilestatusController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
